<?php
declare (strict_types=1);

namespace app\api\controller;

use AlibabaCloud\CDRS\V20201101\PaginateDevice;
use app\BaseController;
use app\common\business\User;


class Login extends Basecontroller
{
    public function index(): object
    {
        if(!$this->request->isPost()){
            return show(config('status.error'), '非法请求!');

        }
        //接受参数
        $phoneNumber = $this->request->param('phone_number', '', 'trim');
        $code = input('param.code', 0, 'intval');
        $type = input('param.type', 0, 'intval');
        //参数校验
        $data = [
            'phone_number' => $phoneNumber,
            'code' => $code,
            'type' => $type,
        ];
        $validate = new \app\api\validate\User();
        $res = $validate->scene('login')->check($data);

        if (!$res) {
            return show(config('status.error'), $validate->getError());
        }

        //调用business层做判断
        $result = (new User())->login($data);

        if ($result) {
            return show(config('status.success'), $result);
        }

        return show(config('status.error'), '登录失败');
    }
}
